<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, user-scalable=no">
  <meta name="description" content="API docs for the startPlayer method from the FlutterSoundPlayer class, for the Dart programming language.">
  <title>startPlayer method - FlutterSoundPlayer class - flutter_sound_player library - Dart API</title>


  
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,300;0,400;0,500;0,700;1,400&display=swap" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0" rel="stylesheet">
  
  <link rel="stylesheet" href="../../static-assets/github.css?v1">
  <link rel="stylesheet" href="../../static-assets/styles.css?v1">
  <link rel="icon" href="../../static-assets/favicon.png?v1">
  
</head>

<body data-base-href="../../" data-using-base-href="false" class="light-theme">
<div id="overlay-under-drawer"></div>
<header id="title">
  <span id="sidenav-left-toggle" class="material-symbols-outlined" role="button" tabindex="0">menu</span>
  <ol class="breadcrumbs gt-separated dark hidden-xs">
    <li><a href="../../index.html">flutter_sound</a></li>
    <li><a href="../../public_flutter_sound_player">public&#47;flutter_sound_player.dart</a></li>
    <li><a href="../../public_flutter_sound_player/FlutterSoundPlayer-class.html">FlutterSoundPlayer</a></li>
    <li class="self-crumb">startPlayer method</li>
  </ol>
  <div class="self-name">startPlayer</div>
  <form class="search navbar-right" role="search">
    <input type="text" id="search-box" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
  <div class="toggle" id="theme-button" title="Toggle brightness">
    <label for="theme">
      <input type="checkbox" id="theme" value="light-theme">
      <span id="dark-theme-button" class="material-symbols-outlined">
        dark_mode
      </span>
      <span id="light-theme-button" class="material-symbols-outlined">
        light_mode
      </span>
    </label>
  </div>
</header>
<main>
<div
    id="dartdoc-main-content"
    class="main-content"
    data-above-sidebar="public_flutter_sound_player&#47;FlutterSoundPlayer-class-sidebar.html"
    data-below-sidebar="">
    <div>
<h1><span class="kind-method">startPlayer</span> method 
</h1></div>

    <section class="multi-line-signature">
      
<span class="returntype"><a href="https://api.flutter.dev/flutter/dart-core/Future-class.html">Future</a><span class="signature">&lt;<wbr><span class="type-parameter"><a href="https://api.flutter.dev/flutter/dart-core/Duration-class.html">Duration</a>?</span>&gt;</span></span>
<span class="name ">startPlayer</span>(<wbr>{<ol class="parameter-list"> <li><span class="parameter" id="startPlayer-param-codec"><span class="type-annotation"><a href="https://pub.dev/documentation/flutter_sound_platform_interface/9.28.0/flutter_sound_platform_interface/Codec.html">Codec</a></span> <span class="parameter-name">codec</span> = <span class="default-value">Codec.aacADTS</span>, </span></li>
<li><span class="parameter" id="startPlayer-param-fromURI"><span class="type-annotation"><a href="https://api.flutter.dev/flutter/dart-core/String-class.html">String</a>?</span> <span class="parameter-name">fromURI</span>, </span></li>
<li><span class="parameter" id="startPlayer-param-fromDataBuffer"><span class="type-annotation"><a href="https://api.flutter.dev/flutter/dart-typed_data/Uint8List-class.html">Uint8List</a>?</span> <span class="parameter-name">fromDataBuffer</span>, </span></li>
<li><span class="parameter" id="startPlayer-param-sampleRate"><span class="type-annotation"><a href="https://api.flutter.dev/flutter/dart-core/int-class.html">int</a></span> <span class="parameter-name">sampleRate</span> = <span class="default-value">16000</span>, </span></li>
<li><span class="parameter" id="startPlayer-param-numChannels"><span class="type-annotation"><a href="https://api.flutter.dev/flutter/dart-core/int-class.html">int</a></span> <span class="parameter-name">numChannels</span> = <span class="default-value">1</span>, </span></li>
<li><span class="parameter" id="startPlayer-param-whenFinished"><span class="type-annotation"><a href="../../public_flutter_sound_player/TWhenFinished.html">TWhenFinished</a>?</span> <span class="parameter-name">whenFinished</span>, </span></li>
</ol>})

      

    </section>
    
<section class="desc markdown">
  <p>Used to play a sound on a player open with <a href="../../public_flutter_sound_player/FlutterSoundPlayer/openPlayer.html">openPlayer()</a>.</p>
<hr>
<p>If you are new to Flutter Sound, you may look to this <a href="/tau/guides/guides_getting-started.html">simple guide</a>.
If you want to play from a dart stream, use the verb <a href="../../public_flutter_sound_player/FlutterSoundPlayer/startPlayerFromStream.html">startPlayerFromStream</a> instead of this verb.</p>
<p>When you want to stop the player, you call the verb <a href="../../public_flutter_sound_player/FlutterSoundPlayer/stopPlayer.html">stopPlayer()</a>.
Note : Flutter Sound does not stop automatically the player when finished.
If you want this, use the <strong><em>whenFinished</em></strong> parameter.</p>
<h2 id="parameters">Parameters</h2>
<ul>
<li><strong><em>codec:</em></strong> is the audio and file format of the file/buffer.
Very often, the <code>codec:</code> parameter is not useful. Flutter Sound will adapt itself depending on the real format of the file provided.
But this parameter is necessary when Flutter Sound must do a format conversion.
Please refer to the <a href="/tau/guides/guides_codecs.html">Codec compatibility Table</a> to know which codecs are currently supported.</li>
<li>One of the two following parameters:
<ul>
<li><strong><em>fromDataBuffer:</em></strong> (if you want to play from a data buffer)</li>
<li><strong><em>fromUri:</em></strong>  (if you want to play a file or a remote URI).
The <code>fromUri</code> parameter, if specified, can be one of three possibilities :
<ul>
<li>The URL of a remote file.</li>
<li>The path of a local file. Hint: <a href="https://pub.dev/packages/path_provider">path_provider</a>
  can be useful if you want to get access to some directories on your device.</li>
<li>The name of a temporary file (without any slash '/').</li>
</ul>
</li>
</ul>
</li>
<li><strong><em>sampleRate:</em></strong> is mandatory with raw PCM codecs (<code>codec</code> == <code>Codec.pcm16</code> or <code>Codec.pcmFloat32</code>). Not used for other codecs.</li>
<li><strong><em>numChannels:</em></strong> is only used with raw PCM codecs (<code>codec</code> == <code>Codec.pcm16</code> or <code>Codec.pcmFloat32</code>). Its value is the number of channels,</li>
<li><strong><em>whenFinished:()</em></strong> : A function for specifying what to do when the playback will be finished.</li>
</ul>
<p>Note: You must specify one or the two parameters : <code>fromUri</code>, <code>fromDataBuffer</code>.</p>
<h2 id="return">Return</h2>
<p><a href="../../public_flutter_sound_player/FlutterSoundPlayer/startPlayer.html">startPlayer()</a> returns a Duration Future, which is the record duration.</p>
<h2 id="examples">Examples</h2>
<p><em>Example:</em></p>
<pre class="language-dart"><code class="language-dart">        Duration d = await myPlayer.startPlayer(
            codec: Codec.aacADTS,
            fromURI: 'foo'
        ); // Play a temporary file

        _playerSubscription = myPlayer.onProgress.listen((e)
        {
                // ...
        });
        myPlayer.setSubscriptionDuration(Duration(milliseconds: 100));
}
</code></pre>
<p><em>Example:</em></p>
<pre class="language-dart"><code class="language-dart">    final fileUri = "https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3";

    Duration d = await myPlayer.startPlayer // Play a remote file
    (
                codec: Codec.mp3,
                fromURI: fileUri,
                whenFinished: ()
                {
                         logger.d( 'I hope you enjoyed listening to this song' );
                         myPlayer.stopPlayer().then ( (_) { setState((){}); );
                },
    );
</code></pre>
<h2 id="see-also">See also</h2>
<ul>
<li><a href="../../public_flutter_sound_player/FlutterSoundPlayer/stopPlayer.html">stopPlayer()</a></li>
<li><a href="../../public_flutter_sound_player/FlutterSoundPlayer/startPlayerFromStream.html">startPlayerFromStream()</a></li>
</ul>
<hr>
</section>


    
<section class="summary source-code" id="source">
  <h2><span>Implementation</span></h2>
  <pre class="language-dart"><code class="language-dart">Future&lt;Duration?&gt; startPlayer({
  Codec codec = Codec.aacADTS,
  String? fromURI,
  Uint8List? fromDataBuffer,
  int sampleRate = 16000, &#47;&#47; Used only with PCM codecs
  int numChannels = 1, &#47;&#47; Used only with PCM codecs
  TWhenFinished? whenFinished,
}) async {
  Duration? r;
  await _lock.synchronized(() async {
    r = await _startPlayer(
      codec: codec,
      fromURI: fromURI,
      fromDataBuffer: fromDataBuffer,
      sampleRate: sampleRate,
      numChannels: numChannels,
      whenFinished: whenFinished,
    );
  }); &#47;&#47; timeout: Duration(seconds: 10));
  return r;
}</code></pre>
</section>


  </div> <!-- /.main-content -->
  <div id="dartdoc-sidebar-left" class="sidebar sidebar-offcanvas-left">
    <!-- The search input and breadcrumbs below are only responsively visible at low resolutions. -->
<header id="header-search-sidebar" class="hidden-l">
  <form class="search-sidebar" role="search">
    <input type="text" id="search-sidebar" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
</header>
<ol class="breadcrumbs gt-separated dark hidden-l" id="sidebar-nav">
    <li><a href="../../index.html">flutter_sound</a></li>
    <li><a href="../../public_flutter_sound_player">flutter_sound_player</a></li>
    <li><a href="../../public_flutter_sound_player/FlutterSoundPlayer-class.html">FlutterSoundPlayer</a></li>
    <li class="self-crumb">startPlayer method</li>
</ol>

    <h5>FlutterSoundPlayer class</h5>
    <div id="dartdoc-sidebar-left-content"></div>
  </div><!--/.sidebar-offcanvas-->
  <div id="dartdoc-sidebar-right" class="sidebar sidebar-offcanvas-right">
</div><!--/.sidebar-offcanvas-->
</main>
<footer>
  <span class="no-break">
    flutter_sound
      9.28.0
  </span>
  
</footer>


<script src="../../static-assets/highlight.pack.js?v1"></script>
<script src="../../static-assets/docs.dart.js"></script>

</body>
</html>

